﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Marbles.Core.Content.Analytics
{
    public class GoogleAnalytics : WebControl, IAnalyticsControl
    {
        public string EngineName
        {
            get { return "google"; }
        }

        public string Title
        {
            get { return "Google Analytics"; }
        }

        public string SupportUrl
        {
            get { return "http://www.google.com/analytics/"; }
        }

        public Dictionary<string, string> Settings
        {
            get
            {
                return _settings;
            }
            set
            {
                _settings = value;
            }
        }

        private Dictionary<string, string> _settings = new Dictionary<string, string>();


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.WriteLine("var _gaq = _gaq || [];");
            writer.WriteLine("_gaq.push(['_setAccount', '" + this.Settings["ACCOUNT"] + "']);");
            writer.WriteLine("_gaq.push(['_trackPageview']);");
            writer.WriteLine("(function() {");
            writer.WriteLine("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
            writer.WriteLine("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
            writer.WriteLine("var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga, s);");
            writer.WriteLine("})();");
            writer.RenderEndTag();
        }
    }
}
